package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.UserCollection;
import com.powernode.model.ProdEs;
import com.powernode.service.UserCollectionService;
import com.powernode.utils.AuthUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 内容:
 * 作者: cy
 */

@RestController
@Api(tags = "会员收藏接口")
@RequestMapping("p/collection")
public class UserCollectController {

    @Autowired
    private UserCollectionService userCollectionService;

    @GetMapping("count")
    @ApiOperation("获取会员的收藏数量")
    public ResponseEntity<Integer> getUserCollectCount() {
        String openId = AuthUtil.getOpenId();
        int count = userCollectionService.count(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, openId)
        );
        return ResponseEntity.ok(count);
}

    @GetMapping("prods")
    @ApiOperation("分页查询会员的收藏商品")
    public ResponseEntity<Page<ProdEs>> getUserCollectProdPage(Page<UserCollection> page) {
        String openId = AuthUtil.getOpenId();
        Page<ProdEs> prodEsPage = userCollectionService.selectUserCollectProdPage(page, openId);
        return ResponseEntity.ok(prodEsPage);
    }
    @GetMapping("isCollection")
    @ApiOperation("查询会员是否收藏该商品")
    public ResponseEntity<Boolean> isCollectProd(Long prodId) {
        String openId = AuthUtil.getOpenId();
        int count = userCollectionService.count(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getProdId, prodId)
                .eq(UserCollection::getUserId, openId)
        );
        return ResponseEntity.ok(count > 0);
    }

    @PostMapping("addOrCancel")
    @ApiOperation("会员新增或者取消商品的收藏")
    public ResponseEntity<Void> addOrCancel(@RequestBody Long prodId) {
        String openId = AuthUtil.getOpenId();
        userCollectionService.addOrCancel(openId, prodId);
        return ResponseEntity.ok().build();
    }

}
